package cunostinte;

import java.util.ArrayList;
import java.util.List;


public class ScenarioAnalzer {
	static List<Record> oameni=new ArrayList<Record>();
	public static String [] cuci=new String[10];
	public static int cont =0;

	public static Record parseLine(String line){
		String persoana="", locatia="", actiune="", ora1="",ora2="",locatia_p="",durata="",linker="",name="";
		String token = " ";
		String[] tmp;
//
		
		//
		Record current = null;
		if(line.startsWith("create")){
			String pers = line.substring(7);
			cuci[cont++]=pers;
			Record om = new Record(pers,"","","","","","","","","","");
			oameni.add(om);
			return om;
		}
		else{
			tmp = line.split(token);
			for (int i=0; i<oameni.size();i++){
				current = oameni.get(i);
				persoana = current.getPersoana();
				if(persoana.equals(tmp[0])){
					persoana = tmp[0];
					for (int j=1; j<tmp.length; j++){
						if(tmp[j].equals("in")){
							j++;
							locatia=tmp[j]; //linker=tmp[j+1];
							
							continue;
						}
						if(tmp[j].equals("at")){
							j+=1;
							ora1=tmp[j]; ora2=tmp[j];
							linker="at";
							continue;}
						else if(tmp[j].equals("From")){
							j+=1;
							ora1 = tmp[j];
							linker="from";
							continue;
						}
						
						if(tmp[j].equals("to")){ ////lalalalalalalalalalalalal
							j+=1;
							ora2 = tmp[j];
							continue;
						}
						if(tmp[j].equals("for")){ ////lalalalalalalalalalalalal
							j+=1;
							durata = tmp[j];
							continue;
						}
						actiune = actiune + tmp[j]+" ";
						name=actiune.replaceAll(" ", "");//+tmp[j];
						//linker=locatia+tmp[j]+" ";
					}
					break;
				}
			}
		}
		actiune=actiune.trim();
		if(actiune.equals("leaves")||actiune.equals("comes")) {
			current.updateLocation("");
		}
		if(actiune.equals("enters")){
			if(!current.getLocation().equals("")){
				locatia_p= current.getLocation();
			}
			current.updateLocation(locatia);
		}
		if(locatia.equals(""))
			locatia=current.getLocation();
		if(ora1.equals(""))
			ora1=ora2;
		String[] time1,time2;
		time1=ora1.split(":");
		time2=ora2.split(":");
		//time3=durata.split(":");
		if((time1.length==2)&&(time2.length==2)){
			return new Record(persoana,actiune,locatia_p,locatia,time1[0],time1[1],time2[0],time2[1],durata,linker, name);
		}else{
		return new Record(persoana,actiune,"",locatia,"","","","","","","");
		}
	}
	
}

